package com.ll.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ll.blog.entity.Article;
import com.ll.blog.entity.Relevancy;
import com.ll.blog.entity.Tag;
import com.ll.blog.mapper.ArticleMapper;
import com.ll.blog.mapper.RelevancyMapper;
import com.ll.blog.model.query.ArticleQuery;
import com.ll.blog.model.query.ArticleTagQuery;
import com.ll.blog.model.res.ArticleDetailRes;
import com.ll.blog.model.res.PageDto;
import com.ll.blog.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2023-10-25
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Autowired
    RelevancyMapper relevancyMapper;
    @Autowired
    ArticleMapper articleMapper;

    @Override
    public PageDto<ArticleDetailRes> getArticlebypage(ArticleQuery query){
        Page<Article> page = Page.of(query.getPage(), query.getPagesize());
        Page<Article> articlePage;
        if (StrUtil.isNotBlank(query.getSortby())){
            page.addOrder(new OrderItem(query.getSortby(),query.getIsAsc()));
            if (!Objects.equals(query.getSortby(), "create_time")){
                page.addOrder(new OrderItem("create_time",false));
            }
        }else{
            page.addOrder(new OrderItem("create_time",false));
        }

        if (query.getClassId() == null){
             articlePage = this.lambdaQuery().eq(Article::getStatus, query.getStatus()).page(page);
        }else{
             articlePage = this.lambdaQuery().eq(Article::getStatus, query.getStatus()).eq(Article::getCategoryId,query.getClassId()).page(page);
        }
        PageDto<ArticleDetailRes> dto = new PageDto<>();
        List<Article> records = articlePage.getRecords();
        dto.setTotal(articlePage.getTotal());
        dto.setPages(articlePage.getPages());
        List<ArticleDetailRes> list = BeanUtil.copyToList(records, ArticleDetailRes.class);
        for (ArticleDetailRes detailReq : list) {
            List<Tag> t = relevancyMapper.getTagList(detailReq.getId());
            detailReq.setTags(t);
        }
        dto.setList(list);
        return dto;
    }
    @Override

    public PageDto<Article> getArticleByTagtoPage(ArticleTagQuery query){
        Integer id = query.getId();
        List<Relevancy> list = relevancyMapper.selectList(new QueryWrapper<Relevancy>().lambda().eq(Relevancy::getTagId, id));
        List<Integer> list1 = new ArrayList<Integer>();
        for (Relevancy relevancy : list) {
            list1.add(relevancy.getArticleId());
        }
        Page<Article> page = Page.of(query.getPage(), query.getPagesize());
        if (StrUtil.isNotBlank(query.getSortby())){
            page.addOrder(new OrderItem(query.getSortby(),query.getIsAsc()));
        }else{
            page.addOrder(new OrderItem("create_time",false));
        }
        Page<Article> p = this.lambdaQuery().in(Article::getId, list1).page(page);
        PageDto<Article> pageDto = new PageDto<>();
        pageDto.setTotal(p.getTotal());
        pageDto.setPages(p.getPages());
        pageDto.setList(p.getRecords());
        return pageDto;
    }


}
